package com.sike.netty.im.client;

import com.sike.netty.im.client.handler.ClientInitializer;
import com.sike.netty.im.client.listener.ReconnectListener;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * 客户端连接运行程序
 *
 * @author chenssy
 * @date 2023/09/26
 */
public class ClientConnectRunner {

    private String host;

    private Integer port;

    private Bootstrap bootstrap;

    public ClientConnectRunner(String host,Integer port) {
        this.host = host;
        this.port = port;

        init();
    }

    public void connect() {
        synchronized (bootstrap) {
            ChannelFuture channelFuture = bootstrap.connect(host,port);
            channelFuture.addListener(new ReconnectListener());
        }
    }

    /**
     * 初始化
     */
    private void init() {
         this.bootstrap = new Bootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ClientInitializer(this));
    }

}
